package com.msg.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.msg.actions.ActionHelper;

/**
 * Servlet implementation class ActionController
 */
@WebServlet("/ActionController")
public class ActionController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	  /**
   * @see HttpServlet#HttpServlet()
   */
	public ActionController() {
		super();
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		ActionHelper actionHelper = new	ActionHelper();
	//	System.out.println("ActionController Servlet called"+request.getParameter("requestAction"));
	//	System.out.println("ActionController Servlet called"+request.getParameter("season"));
		String jsonString = null;
		
			if(request.getParameter("requestAction").equals("events"))		
				 jsonString = actionHelper.getEventData();
			else if(request.getParameter("requestAction").equals("forecast"))	
				jsonString = actionHelper.getForecastData(request.getParameter("event"));
			else if(request.getParameter("requestAction").equals("season"))
			{	 if(request.getParameter("season").equals("ALL"))
					 jsonString = actionHelper.getSeasons(); 
				 else	 
					 jsonString = actionHelper.getSeasonData(request.getParameter("season"));
			}
			else if(request.getParameter("requestAction").equals("seasonforecasts"))	
				 jsonString = actionHelper.getSeasonForecastData();
			else if(request.getParameter("requestAction").equals("ticket"))	
			 jsonString = actionHelper.getTicketData();
			else if(request.getParameter("requestAction").equals("prices"))	
				 jsonString = actionHelper.getPriceData();
			else if(request.getParameter("requestAction").equals("ticketgroups"))	
				 jsonString = actionHelper.getTicketGroupData();
			else if(request.getParameter("requestAction").equals("ticketGraph"))
				jsonString = actionHelper.getGraphData();
			else if(request.getParameter("requestAction").equals("forecastGraph"))
				jsonString = actionHelper.getForecastGraph(request.getParameter("event"));
		
		
		response.setContentType("application/json");
		System.out.println(jsonString);
		response.getWriter().write(jsonString);
		response.getWriter().flush();
		response.getWriter().close();
	}
}